package com.feri.gpstudy.service.impl;

import com.feri.gpstudy.bo.MedicationAllBo;
import com.feri.gpstudy.dao.MedicationDao;
import com.feri.gpstudy.entity.Medication;
import com.feri.gpstudy.service.intf.MedicationService;
import com.feri.gpstudy.vo.R;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Service
public class MedicationServiceImpl implements MedicationService {
    @Resource
    private MedicationDao dao;

    @Override
    public R save(Medication medication) {
        //1. 操作数据库
        if (dao.insert(medication)>0){
            return R.ok();
        }
        return R.fail("亲，您增加的药品数据错误");
    }

    @Override
    public R queryAll(MedicationAllBo bo) {
        PageHelper.startPage(bo.getPage(), bo.getSize());
        PageInfo<Medication> pageInfo=new PageInfo<>(dao.selectAll());
        return R.ok(pageInfo);
    }

    @Override
    public R update(Medication medication) {
        if (medication != null){
            int i = 0;
            i = dao.update(medication);
            if (i > 0){
                return R.ok();
            }
        }
        return R.fail("亲，你输入的数据有误");
    }

    @Override
    public R del(long mid) {
        if (mid>0){
            int i= dao.del(mid);
            if(i>0){
                return R.ok();
            }
        }
        return R.fail("删除失败");
    }


}
